/*
 * @Description: localStorage存储
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-04-06
 * @LastEditors: Bart 1714877505@qq.com
 * @LastEditTime: 2025-04-25
 */

const TOKEN_KEY = 'token'
const PLATFORMCODE_KEY = 'platformCode'
const COMPANY_INFO_KEY = 'companyInfo'
const COMPANY_LIST_KEY = 'companyList'
const MODULELIST_KEY = 'moduleList'
const USER_ID = 'userId'
const PERSON_DATA = 'personalData'
const IS_APPROVE = 'isApprove'
const EXCHANGE_ACCOUNT = 'exchangeAccount'

/**
 * 存储token
 * @param token 用户令牌
 */
export const saveToken = (token: string) => localStorage.setItem(TOKEN_KEY, token)

/**
 * 获取token
 * @return
 */
export const getToken = () => localStorage.getItem(TOKEN_KEY) as string

/**
 * 清空token
 */
export const clearToken = () => localStorage.removeItem(TOKEN_KEY)

/**
 * 存储userId
 * @param userId 用户Id
 */
export const saveUserId = (userId: string) => localStorage.setItem(USER_ID, userId)

/**
 * 获取userId
 * @return
 */
export const getUserId = () => localStorage.getItem(USER_ID) as string

/**
 * 存储平台编码
 * @param token 用户令牌
 */
export const savePlatformCode = (platformCode: string) => localStorage.setItem(PLATFORMCODE_KEY, platformCode)

/**
 * 获取平台编码
 * @return
 */
export const getPlatformCode = () => localStorage.getItem(PLATFORMCODE_KEY) as string

/**
 * 清空平台编码
 */
export const clearPlatformCode = () => localStorage.removeItem(PLATFORMCODE_KEY)

/**
 * 存储个人数据
 * @param token 个人数据
 */
export const savePersonData = (personData: IPersonData) => localStorage.setItem(PERSON_DATA, JSON.stringify(personData ?? {}))

/**
 * 获取个人数据
 * @return
 */
export const getPersonData = () => JSON.parse(localStorage.getItem(PERSON_DATA) ?? '{}')

/**
 * 清空个人数据
 */
export const clearPersonData = () => localStorage.removeItem(PERSON_DATA)

/**
 * 存储机构信息
 * @param companyInfo 机构信息
 */
export const saveCompanyInfo = (companyInfo: ICompanyInfo) => localStorage.setItem(COMPANY_INFO_KEY, JSON.stringify(companyInfo ?? {}))

/**
 * 获取机构信息
 * @return
 */
export const getCompanyInfo = (): ICompanyInfo => JSON.parse(localStorage.getItem(COMPANY_INFO_KEY) ?? '{}')

/**
 * 清空机构信息
 */
export const clearCompanyInfo = () => localStorage.removeItem(COMPANY_INFO_KEY)

/**
 * 存储机构列表
 */
export const saveCompanyList = (companyInfo: GetUserCompanyListTypes.IResponse[]) => localStorage.setItem(COMPANY_LIST_KEY, JSON.stringify(companyInfo ?? []))

/**
 * 获取机构列表
 */
export const getCompanyList = (): GetUserCompanyListTypes.IResponse[] => JSON.parse(localStorage.getItem(COMPANY_LIST_KEY) ?? '[]')

/**
 * 清空机构列表
 */
export const clearCompanyList = () => localStorage.removeItem(COMPANY_LIST_KEY)

/**
 * 存储模块列表
 */
export const saveModuleList = (moduleList: ModuleListTypes.IResponse[]) => localStorage.setItem(MODULELIST_KEY, JSON.stringify(moduleList ?? []))

/**
 * 获取模块列表
 */
export const getModuleList = (): ModuleListTypes.IResponse[] => JSON.parse(localStorage.getItem(MODULELIST_KEY) ?? '[]')

/**
 * 清空模块列表
 */
export const clearModuleList = () => localStorage.removeItem(MODULELIST_KEY)

/**
 * 存储是否审核
 */
export const saveIsApprove = (isApprove: string) => localStorage.setItem(IS_APPROVE, isApprove)

/**
 * 获取是否审核
 */
export const getIsApprove = () => localStorage.getItem(IS_APPROVE)
/**
 * 清空是否审核
 */
export const clearIsApprove = () => localStorage.removeItem(IS_APPROVE)

/**
 * 存储交易所账户数据
 * @param token 交易所账户数据
 */
export const saveExchangeAccount = (exchangeAccount: AccountTypes.IResponse) => localStorage.setItem(EXCHANGE_ACCOUNT, JSON.stringify(exchangeAccount ?? {}))

/**
 * 获取交易所账户数据
 * @return
 */
export const getExchangeAccount = () => JSON.parse(localStorage.getItem(EXCHANGE_ACCOUNT) ?? '{}')

/**
 * 清空交易所账户数据
 */
export const clearExchangeAccount = () => localStorage.removeItem(EXCHANGE_ACCOUNT)
